package com.chalk.util.amap;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

/**
 * 高德地图工具类
 */
public class AmapUtil {
    private   static String key="0347c746c0bdaca2a76edc8b6c5fe569";

    public static void main(String[] args) {
        long start=System.currentTimeMillis();
        for(int i=0;i<1000;i++) {
            String startLonLat = "116.413731,39.979324";
            String endLonLat = "116.417537,39.97722";
            //System.out.println(startLonLat);
          //  System.out.println(endLonLat);
            Long dis = getDistance(startLonLat, endLonLat);
          //  System.out.println(dis);
        }
        long end=System.currentTimeMillis();
        System.out.println("耗时:"+(end-start));
       // System.out.println(getAddressLocation("厦门五通灯塔足球训练场")[0]);
//        String str="aabTbcc";
//        System.out.println(str.replaceAll("T",""));



    }
    public static Long getDistance(String startLonLat, String endLonLat){
        //返回起始地startAddr与目的地endAddr之间的距离，单位：米
        Long result = 0L;
        String queryUrl = "http://restapi.amap.com/v3/distance?key="+key+"&origins="+startLonLat+"&destination="+endLonLat;
        String queryResult = getResponse(queryUrl);
        System.out.println(queryResult);
        JSONObject jo = JSONObject.fromObject(queryResult);
        JSONArray ja = jo.getJSONArray("results");
        Object obj=JSONObject.fromObject(ja.getString(0)).get("distance");
        if(obj==null){
            return result;
        }
        result = Long.parseLong(obj.toString());
        return result;
    }

    private static String getResponse(String serverUrl){
        //用JAVA发起http请求，并返回json格式的结果
        StringBuffer result = new StringBuffer();
        try {
            URL url = new URL(serverUrl);
            URLConnection conn = url.openConnection();
            BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));

            String line;
            while((line = in.readLine()) != null){
                result.append(line);
            }
            in.close();

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result.toString();
    }

    public static String[] getAddressLocation(String address){
        String url="https://restapi.amap.com/v3/geocode/geo?key="+key+"&address="+address;
        String result=getResponse(url);
        System.out.println(result);
        JSONObject jo = JSONObject.fromObject(result);
        JSONArray jsonArray = jo.getJSONArray("geocodes");
        if(jsonArray.size()==0){
            return new String[2];
        }
        JSONObject zjo=  jsonArray.getJSONObject(0);
         String location= zjo.getString("location");

        return location.split(",");
    }

}
